package com.live.dengdengtv.activity.support;

import android.util.Log;
import android.widget.BaseAdapter;

import com.live.dengdengtv.bean.gson.CustomMsg;
import com.live.dengdengtv.kernel.avcontrollers.QavsdkControl;
import com.live.dengdengtv.kernel.utils.SxbLog;
import com.tencent.TIMFriendshipManager;
import com.tencent.TIMGroupManager;
import com.tencent.TIMGroupMemberInfo;
import com.tencent.TIMUserProfile;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 16/5/24.
 */
public class LiveMemberListHelper {
    private static final String TAG = LiveMemberListHelper.class.getSimpleName();

    private List<CustomMsg.CustomUserInfo> romMembers;
    private BaseAdapter mAdapter;
    public LiveMemberListHelper(BaseAdapter adapter, List<CustomMsg.CustomUserInfo> romMembers) {
        mAdapter = adapter;
        this.romMembers = romMembers;
    }

    public void getMemberList() {
        TIMGroupManager.getInstance().getGroupMembers("" + CurLiveInfo.getRoomNum(), new TIMValueCallBack<List<TIMGroupMemberInfo>>() {
            @Override
            public void onError(int i, String s) {

                SxbLog.i(TAG, "get MemberList ");
            }

            @Override
            public void onSuccess(List<TIMGroupMemberInfo> timGroupMemberInfos) {
                SxbLog.i(TAG, "get MemberList ");
                getMemberListInfo(timGroupMemberInfos);
            }
        });
    }

    private void getMemberListInfo(List<TIMGroupMemberInfo> timGroupMemberInfos){

        List<String> users = new ArrayList<String>();
        for (TIMGroupMemberInfo item : timGroupMemberInfos) {
            if (item.getUser().equals(CurLiveInfo.getHostID())) {
                continue;
            }
            users.add(item.getUser());
        }

        if(users.size()<=0) return;

        TIMFriendshipManager.getInstance().getUsersProfile(users, new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getMemberListInfo failed: " + code + " desc" + desc);
            }
            @Override
            public void onSuccess(List<TIMUserProfile> timUserProfilesList) {
                for(TIMUserProfile item : timUserProfilesList){
                    CustomMsg.CustomUserInfo member = new CustomMsg.CustomUserInfo();
                    member.userId = item.getIdentifier();
                    member.nickName = item.getNickName();
                    member.faceUrl = item.getFaceUrl();
                    member.sex = item.getGender().getValue() +"";
                    member.selfSign = item.getSelfSignature();

                    if (QavsdkControl.getInstance().containIdView(item.getIdentifier())) {
                        member.isOnVideoChat = true;
                    }
                    romMembers.add(member);
                }
                mAdapter.notifyDataSetChanged();
            }
        });

    }

}
